﻿//==============================================================================
// File: PresenterBase.cs
// Created: 2010-06-07
// Author: Piotr Włodek, Arkadiusz Świerczek
//==============================================================================
// This file is a part of MAMMOTH project.
// Copyright (C) 2010 AGH University of Science and Technology, Krakow.
// https://caribou.iisg.agh.edu.pl/trac/mammoth
//
// Mammoth is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// Mammoth is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public License
// along with Mammoth. If not, see http://www.gnu.org/licenses/.
//==============================================================================

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using WpfRcp.Core.Common;

namespace WpfRcp.Core.Views.Presenters
{
    /// <summary>
    /// Presneter for the Model-View-Presenter and PresentationModel design patterns.
    /// </summary>
    /// <typeparam name="TView">Concrete view type.</typeparam>
    public abstract class PresenterBase<TView> : INotifyPropertyChanged, IDisposable
        where TView : class
    {
        private TView m_View;

        /// <summary>
        /// Gets or sets the view controlled by the presenter.
        /// Bear in mind that the view can only be set once.
        /// </summary>
        public TView View
        {
            get { return m_View; }
            set
            {
                if (m_View != null)
                    throw new InvalidOperationException("View can only be set once.");

                m_View = value;
                OnPropertyChanged<PresenterBase<TView>>(t => t.View);

                // After setting the view, initialize it
                Initialize();
            }
        }

        protected virtual void Initialize()
        {
            // Do nothing
        }

        #region INotifyPropertyChanged Implementation

        [field: NonSerialized]
        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }

        protected void OnPropertyChanged<T>(Expression<Func<T, object>> property)
        {
            OnPropertyChanged(typeof(T).GetPropertyName(property));
        }

        #endregion

        #region IDisposable Implementation

        private bool m_Disposed;

        public bool Disposed
        {
            get
            {
                lock (this)
                {
                    return m_Disposed; 
                }
            }
        }

        public void Dispose()
        {
            lock (this)
            {
                if (m_Disposed == false)
                {
                    Dispose(true);
                    m_Disposed = true;

                    GC.SuppressFinalize(this);
                }
            }
        }

        protected virtual void Dispose(bool disposing)
        {
            CleanupDebugInfo();
        }

        [Conditional("DEBUG")]
        private void CleanupDebugInfo()
        {
            var msg = string.Format("Finalizing {0} ({1})", GetType(), GetHashCode());
            Debug.WriteLine(msg);
        }

        ~PresenterBase()
        {
            Dispose(false);
        }

        #endregion
    }
}